package fm.jiecao.jcvideoplayer_lib;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;

@SuppressLint("AppCompatCustomView")
public class MySeekBar extends SeekBar {
    //比例对应的原点分辨率
    private int ScreenWidth = 720;
    private int screenHeight = 1280;
    private Drawable thumb;
    private Resources res;
    private Paint paint;
    private Bitmap bmp;
    private Drawable mThumb;
    private int mThumbWidth;

    public MySeekBar(Context context) {
        this(context, null);
    }

    @SuppressWarnings("deprecation")
    public MySeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true);
        paint.setColor(getResources().getColor(R.color.white));
        res = context.getResources();
        mThumbWidth=res.getDimensionPixelSize(R.dimen.thumb_width);
        /*if(Pixels.getpixels_x(100) == 720){
            bmp = BitmapFactory.decodeResource(res, R.drawable.backgroud_thumb);
        }else{

        }*/
        bmp = BitmapFactory.decodeResource(res, R.drawable.backgroud_thumb);
        thumb = new BitmapDrawable(bmp);

        paint.setTextSize(30);
        // 设置拖动的图片
        //setThumb(thumb);
        // 图片的位置
        setThumbOffset(thumb.getIntrinsicWidth());
    }

    @Override
    public void setThumb(Drawable thumb) {
        // TODO Auto-generated method stub
        super.setThumb(thumb);
        this.mThumb = thumb;
    }
    public Drawable getSeekBarThumb() {
        return mThumb;
    }

    //设置thumb的偏移数值
    @Override
    public void setThumbOffset(int thumbOffset) {
        // TODO Auto-generated method stub
        super.setThumbOffset(thumbOffset/3);
    }

    String temp_str ;

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.save();
        Rect rect = getSeekBarThumb().getBounds();
        Rect textRect = new Rect();
        if(temp_str==null){
            return;
        }
        paint.getTextBounds(temp_str, 0, temp_str.length(), textRect);
        float fontwidth = paint.measureText(temp_str);
        float Textx=mThumbWidth/2+fontwidth/2-38;
        Paint.FontMetrics fontMetrics=paint.getFontMetrics();
        float dy=(fontMetrics.descent-fontMetrics.ascent)/2-fontMetrics.descent;
        float Texty=rect.height()/2+dy;
        canvas.drawText(temp_str, rect.left+Textx , Texty, paint);
        canvas.restore();
    }

    public void setmThumbText(String thumbText){
        temp_str=thumbText;
        //setProgress(getProgress());
        invalidate();
    }

    private Bitmap drawableToBitmap(Drawable drawable) {
        // 取 drawable 的长宽
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        // 取 drawable 的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 建立对应 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }

    public void SetValue(String value) {
        StringBuffer sb = new StringBuffer();
        sb.append(value);
        temp_str = sb.toString();
        invalidate();
    }

    @SuppressLint("NewApi")
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void setOnSeekBarChangeListener(final OnSeekBarChangeListener l) {
        super.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (l != null) {
                    l.onProgressChanged(seekBar, progress, fromUser);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                if (l != null) {
                    l.onStartTrackingTouch(seekBar);
                }
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (l != null) {
                    l.onStopTrackingTouch(seekBar);
                }
            }
        });
    }
}
